package com.ruoyi.comprehensive.service.impl;

import java.util.Date;
import java.util.List;
import java.util.Random;
import java.util.stream.Collectors;

import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.comprehensive.domain.GasPreventionExamine;
import com.ruoyi.comprehensive.domain.PreventionEnclosure;
import com.ruoyi.comprehensive.mapper.GasPreventionExamineMapper;
import com.ruoyi.comprehensive.mapper.PreventionEnclosureMapper;
import com.ruoyi.comprehensive.dto.GasPreventionDto;
import com.ruoyi.comprehensive.service.ICodeService;
import com.ruoyi.comprehensive.service.IGasPreventionExamineService;
import com.ruoyi.comprehensive.vo.CodeVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.comprehensive.mapper.GasPreventionMapper;
import com.ruoyi.comprehensive.domain.GasPrevention;
import com.ruoyi.comprehensive.service.IGasPreventionService;

/**
 * 瓦斯管理Service业务层处理
 * 
 * @author hcc
 * @date 2023-12-14
 */
@Service
public class GasPreventionServiceImpl implements IGasPreventionService 
{
    @Autowired
    private ICodeService codeService;
    @Autowired
    private GasPreventionMapper gasPreventionMapper;
    @Autowired
    private PreventionEnclosureMapper preventionEnclosureMapper;
    @Autowired
    private IGasPreventionExamineService gasPreventionExamineService;

    /**
     * 查询瓦斯管理
     * 
     * @param id 瓦斯管理主键
     * @return 瓦斯管理
     */
    @Override
    public GasPrevention selectGasPreventionById(String id)
    {
        return gasPreventionMapper.selectGasPreventionById(id);
    }

    /**
     * 查询瓦斯管理列表
     * 
     * @param gasPrevention 瓦斯管理
     * @return 瓦斯管理
     */
    @Override
    public List<GasPrevention> selectGasPreventionList(GasPrevention gasPrevention)
    {
        return gasPreventionMapper.selectGasPreventionList(gasPrevention);
    }

    /**
     * 新增瓦斯管理
     * 
     * @param gasPrevention 瓦斯管理
     * @return 结果
     */
    @Override
    public int insertGasPrevention(GasPrevention gasPrevention)
    {

        GasPrevention gasPrevention1 = new GasPrevention();
        BeanUtils.copyProperties(gasPrevention,gasPrevention1);
        gasPrevention1.setExamineStatus(0L);
        CodeVo codeVo = new CodeVo();
        codeVo.setPrefix("TFWS");
        Random random = new Random();
        codeVo.setClickCount(random.nextInt(100));
        gasPrevention1.setGasPreventionCode(codeService.getDustPreventionCode(codeVo));
        if (gasPrevention1.getTeamGroup() != null){
            gasPrevention1.setReportForm("Y");
        }else {
            gasPrevention1.setReportForm("D");
        }
        gasPrevention1.setCreateByName(SecurityUtils.getUsername());
        gasPrevention1.setCreateBy(SecurityUtils.getUserId().toString());
        gasPrevention1.setCreateAt(new Date());
        gasPrevention1.setUpdateAt(new Date());
        gasPrevention1.setUpdateByName(SecurityUtils.getUsername());
        gasPrevention1.setUpdateBy(SecurityUtils.getUserId().toString());
        return gasPreventionMapper.insertGasPrevention(gasPrevention1);
    }

    /**
     * 修改瓦斯管理
     * 
     * @param gasPrevention 瓦斯管理
     * @return 结果
     */
    @Override
    public int updateGasPrevention(GasPrevention gasPrevention)
    {
        if (gasPrevention.getExamineStatus() != null && gasPrevention.getExamineStatus() == 1L){
            GasPrevention gasPrevention1 = gasPreventionMapper.selectGasPreventionById(gasPrevention.getId());
            if (gasPrevention1 != null){
                GasPreventionExamine gasPreventionExamine = new GasPreventionExamine();
                BeanUtils.copyProperties(gasPrevention1,gasPreventionExamine);
                gasPreventionExamine.setGasPreventionId(gasPrevention1.getId());
                gasPreventionExamine.setExamineStatus(1L);
                gasPreventionExamineService.insertGasPreventionExamine(gasPreventionExamine);
            }
        }
        return gasPreventionMapper.updateGasPrevention(gasPrevention);
    }

    /**
     * 批量删除瓦斯管理
     * 
     * @param ids 需要删除的瓦斯管理主键
     * @return 结果
     */
    @Override
    public int deleteGasPreventionByIds(String[] ids)
    {
        return gasPreventionMapper.deleteGasPreventionByIds(ids);
    }

    /**
     * 删除瓦斯管理信息
     * 
     * @param id 瓦斯管理主键
     * @return 结果
     */
    @Override
    public int deleteGasPreventionById(String id)
    {
        return gasPreventionMapper.deleteGasPreventionById(id);
    }

    /**
     * 获取瓦斯管理详细信息
     * @param id
     * @return
     */
    @Override
    public GasPreventionDto getGasPreventionById(String id) {
        GasPreventionDto gasPreventionDto = new GasPreventionDto();
        // 通过ID获取瓦斯信息
        GasPrevention gasPrevention = gasPreventionMapper.selectGasPreventionById(id);
        PreventionEnclosure preventionEnclosure = new PreventionEnclosure();
        preventionEnclosure.setPreventionId(gasPrevention.getId());
        preventionEnclosure.setEnclosureType("4");
        //对象赋值
        BeanUtils.copyProperties(gasPrevention, gasPreventionDto);
        // 查询附件信息内容
        List<PreventionEnclosure> preventionEnclosures = preventionEnclosureMapper.selectPreventionEnclosureList(preventionEnclosure);
        gasPreventionDto.setPreventionEnclosures(preventionEnclosures);
        return gasPreventionDto;
    }

    /**
     * 查询瓦斯管理列表
     * @param gasPrevention
     * @return
     */
    @Override
    public List<GasPreventionDto> getGasPreventionList(GasPrevention gasPrevention) {
        List<GasPreventionDto> gasPreventionList = gasPreventionMapper.getGasPreventionList(gasPrevention);
        return gasPreventionList;
    }
}
